<!DOCTYPE HTML>
<html lang="zh-cn" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Loader | Webpack 中文指南</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.5.2">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-ad/plugin.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./configuration.html" />
    
    
    <link rel="prev" href="./usage.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="2.3" data-chapter-title="Loader" data-filepath="loader.md" data-basepath="." data-revision="Wed May 03 2017 21:11:01 GMT+0800 (CST)">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="preface.html">
            
                
                    <a href="./preface.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        前言
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="module-system.html">
            
                
                    <a href="./module-system.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        模块系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="what-is-webpack.html">
            
                
                    <a href="./what-is-webpack.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        重复的轮子
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="start.html">
            
                
                    <a href="./start.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        准备开始
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="install.html">
            
                
                    <a href="./install.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="usage.html">
            
                
                    <a href="./usage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        使用
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.3" data-path="loader.html">
            
                
                    <a href="./loader.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Loader
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="configuration.html">
            
                
                    <a href="./configuration.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="plugin.html">
            
                
                    <a href="./plugin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        插件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="development.html">
            
                
                    <a href="./development.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="troubleshooting.html">
            
                
                    <a href="./troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        故障处理
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="advanced.html">
            
                
                    <a href="./advanced.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="commonjs.html">
            
                
                    <a href="./commonjs.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        CommonJS 规范
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="amd.html">
            
                
                    <a href="./amd.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        AMD 规范
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="reference.html">
            
                
                    <a href="./reference.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        参考链接
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Webpack 中文指南</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="loader">Loader</h1>
<p>Webpack &#x672C;&#x8EAB;&#x53EA;&#x80FD;&#x5904;&#x7406; JavaScript &#x6A21;&#x5757;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x5904;&#x7406;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x4F7F;&#x7528; loader &#x8FDB;&#x884C;&#x8F6C;&#x6362;&#x3002;</p>
<p>Loader &#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x662F;&#x6A21;&#x5757;&#x548C;&#x8D44;&#x6E90;&#x7684;&#x8F6C;&#x6362;&#x5668;&#xFF0C;&#x5B83;&#x672C;&#x8EAB;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x63A5;&#x53D7;&#x6E90;&#x6587;&#x4EF6;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x8F6C;&#x6362;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>require</code> &#x6765;&#x52A0;&#x8F7D;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x7684;&#x6A21;&#x5757;&#x6216;&#x6587;&#x4EF6;&#xFF0C;&#x6BD4;&#x5982; CoffeeScript&#x3001; JSX&#x3001; LESS &#x6216;&#x56FE;&#x7247;&#x3002;</p>
<p>&#x5148;&#x6765;&#x770B;&#x770B; loader &#x6709;&#x54EA;&#x4E9B;&#x7279;&#x6027;&#xFF1F;</p>
<ul>
<li>Loader &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7BA1;&#x9053;&#x65B9;&#x5F0F;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#xFF0C;&#x6BCF;&#x4E2A; loader &#x53EF;&#x4EE5;&#x628A;&#x8D44;&#x6E90;&#x8F6C;&#x6362;&#x6210;&#x4EFB;&#x610F;&#x683C;&#x5F0F;&#x5E76;&#x4F20;&#x9012;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A; loader &#xFF0C;&#x4F46;&#x662F;&#x6700;&#x540E;&#x4E00;&#x4E2A; loader &#x5FC5;&#x987B;&#x8FD4;&#x56DE; JavaScript&#x3002;</li>
<li>Loader &#x53EF;&#x4EE5;&#x540C;&#x6B65;&#x6216;&#x5F02;&#x6B65;&#x6267;&#x884C;&#x3002;</li>
<li>Loader &#x8FD0;&#x884C;&#x5728; node.js &#x73AF;&#x5883;&#x4E2D;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x505A;&#x4EFB;&#x4F55;&#x53EF;&#x80FD;&#x7684;&#x4E8B;&#x60C5;&#x3002;</li>
<li>Loader &#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x53C2;&#x6570;&#xFF0C;&#x4EE5;&#x6B64;&#x6765;&#x4F20;&#x9012;&#x914D;&#x7F6E;&#x9879;&#x7ED9; loader&#x3002;</li>
<li>Loader &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6587;&#x4EF6;&#x6269;&#x5C55;&#x540D;&#xFF08;&#x6216;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#xFF09;&#x7ED1;&#x5B9A;&#x7ED9;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x6587;&#x4EF6;&#x3002;</li>
<li>Loader &#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>npm</code> &#x53D1;&#x5E03;&#x548C;&#x5B89;&#x88C5;&#x3002;</li>
<li>&#x9664;&#x4E86;&#x901A;&#x8FC7; <code>package.json</code> &#x7684; <code>main</code> &#x6307;&#x5B9A;&#xFF0C;&#x901A;&#x5E38;&#x7684;&#x6A21;&#x5757;&#x4E5F;&#x53EF;&#x4EE5;&#x5BFC;&#x51FA;&#x4E00;&#x4E2A; loader &#x6765;&#x4F7F;&#x7528;&#x3002;</li>
<li>Loader &#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x914D;&#x7F6E;&#x3002;</li>
<li>&#x63D2;&#x4EF6;&#x53EF;&#x4EE5;&#x8BA9; loader &#x62E5;&#x6709;&#x66F4;&#x591A;&#x7279;&#x6027;&#x3002;</li>
<li>Loader &#x53EF;&#x4EE5;&#x5206;&#x53D1;&#x51FA;&#x9644;&#x52A0;&#x7684;&#x4EFB;&#x610F;&#x6587;&#x4EF6;&#x3002;</li>
</ul>
<p>Loader &#x672C;&#x8EAB;&#x4E5F;&#x662F;&#x8FD0;&#x884C;&#x5728; node.js &#x73AF;&#x5883;&#x4E2D;&#x7684; JavaScript &#x6A21;&#x5757;&#xFF0C;&#x5B83;&#x901A;&#x5E38;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7; npm &#x6765;&#x7BA1;&#x7406; loader&#xFF0C;&#x4F46;&#x662F;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x81EA;&#x5DF1;&#x5199; loader &#x6A21;&#x5757;&#x3002;</p>
<p>&#x6309;&#x7167;&#x60EF;&#x4F8B;&#xFF0C;&#x800C;&#x975E;&#x5FC5;&#x987B;&#xFF0C;loader &#x4E00;&#x822C;&#x4EE5; <code>xxx-loader</code> &#x7684;&#x65B9;&#x5F0F;&#x547D;&#x540D;&#xFF0C;<code>xxx</code> &#x4EE3;&#x8868;&#x4E86;&#x8FD9;&#x4E2A; loader &#x8981;&#x505A;&#x7684;&#x8F6C;&#x6362;&#x529F;&#x80FD;&#xFF0C;&#x6BD4;&#x5982; <code>json-loader</code>&#x3002;</p>
<p>&#x5728;&#x5F15;&#x7528; loader &#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5168;&#x540D; <code>json-loader</code>&#xFF0C;&#x6216;&#x8005;&#x4F7F;&#x7528;&#x77ED;&#x540D; <code>json</code>&#x3002;&#x8FD9;&#x4E2A;&#x547D;&#x540D;&#x89C4;&#x5219;&#x548C;&#x641C;&#x7D22;&#x4F18;&#x5148;&#x7EA7;&#x987A;&#x5E8F;&#x5728; webpack &#x7684; <code>resolveLoader.moduleTemplates</code> api &#x4E2D;&#x5B9A;&#x4E49;&#x3002;</p>
<pre><code>Default: [&quot;*-webpack-loader&quot;, &quot;*-web-loader&quot;, &quot;*-loader&quot;, &quot;*&quot;]
</code></pre><p>Loader &#x53EF;&#x4EE5;&#x5728; <code>require()</code> &#x5F15;&#x7528;&#x6A21;&#x5757;&#x7684;&#x65F6;&#x5019;&#x6DFB;&#x52A0;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5728; webpack &#x5168;&#x5C40;&#x914D;&#x7F6E;&#x4E2D;&#x8FDB;&#x884C;&#x7ED1;&#x5B9A;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x7684;&#x65B9;&#x5F0F;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x63A5;&#x4E0A;&#x4E00;&#x8282;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x5728;&#x9875;&#x9762;&#x4E2D;&#x5F15;&#x5165;&#x4E00;&#x4E2A; CSS &#x6587;&#x4EF6; style.css&#xFF0C;&#x9996;&#x9875;&#x5C06; style.css &#x4E5F;&#x770B;&#x6210;&#x662F;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x7136;&#x540E;&#x7528; <code>css-loader</code> &#x6765;&#x8BFB;&#x53D6;&#x5B83;&#xFF0C;&#x518D;&#x7528; <code>style-loader</code> &#x628A;&#x5B83;&#x63D2;&#x5165;&#x5230;&#x9875;&#x9762;&#x4E2D;&#x3002;</p>
<pre><code class="lang-css"><span class="hljs-comment">/* style.css */</span>
<span class="hljs-tag">body</span> <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">background</span>:<span class="hljs-value"> yellow</span></span>; }</span>
</code></pre>
<p>&#x4FEE;&#x6539; entry.js&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;!style-loader!css-loader!./style.css&quot;</span>) <span class="hljs-comment">// &#x8F7D;&#x5165; style.css</span>
<span class="hljs-built_in">document</span>.write(<span class="hljs-string">&apos;It works.&apos;</span>)
<span class="hljs-built_in">document</span>.write(<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./module.js&apos;</span>))
</code></pre>
<p>&#x5B89;&#x88C5; loader&#xFF1A;</p>
<pre><code class="lang-bash">npm install css-loader style-loader
</code></pre>
<p>&#x91CD;&#x65B0;&#x7F16;&#x8BD1;&#x6253;&#x5305;&#xFF0C;&#x5237;&#x65B0;&#x9875;&#x9762;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x9EC4;&#x8272;&#x7684;&#x9875;&#x9762;&#x80CC;&#x666F;&#x4E86;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6BCF;&#x6B21; <code>require</code> CSS &#x6587;&#x4EF6;&#x7684;&#x65F6;&#x5019;&#x90FD;&#x8981;&#x5199; loader &#x524D;&#x7F00;&#xFF0C;&#x662F;&#x4E00;&#x4EF6;&#x5F88;&#x7E41;&#x7410;&#x7684;&#x4E8B;&#x60C5;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x6A21;&#x5757;&#x7C7B;&#x578B;&#xFF08;&#x6269;&#x5C55;&#x540D;&#xFF09;&#x6765;&#x81EA;&#x52A8;&#x7ED1;&#x5B9A;&#x9700;&#x8981;&#x7684; loader&#x3002;</p>
<p>&#x5C06; entry.js &#x4E2D;&#x7684; <code>require(&quot;!style!css!./style.css&quot;)</code> &#x4FEE;&#x6539;&#x4E3A; <code>require(&quot;./style.css&quot;)</code> &#xFF0C;&#x7136;&#x540E;&#x6267;&#x884C;&#xFF1A;</p>
<pre><code class="lang-bash">$ webpack entry.js bundle.js --module-bind <span class="hljs-string">&apos;css=style-loader!css-loader&apos;</span>

<span class="hljs-comment"># &#x6709;&#x4E9B;&#x73AF;&#x5883;&#x4E0B;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x53CC;&#x5F15;&#x53F7;</span>
$ webpack entry.js bundle.js --module-bind <span class="hljs-string">&quot;css=style-loader!css-loader&quot;</span>
</code></pre>
<p>&#x663E;&#x7136;&#xFF0C;&#x8FD9;&#x4E24;&#x79CD;&#x4F7F;&#x7528; loader &#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x6548;&#x679C;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./usage.html" class="navigation navigation-prev " aria-label="Previous page: 使用"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./configuration.html" class="navigation navigation-next " aria-label="Next page: 配置文件"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-github/plugin.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-editlink/plugin.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-baidu/plugin.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-ad/plugin.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"github":{"url":"https://github.com/zhaoda/webpack-handbook"},"editlink":{"base":"https://github.com/zhaoda/webpack-handbook/edit/master/content","label":"编辑本页","multilingual":false},"baidu":{"token":"a9787f0ab45d5e237bab522431d0a7ec"},"sharing":{"weibo":true,"twitter":false,"facebook":false,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"ad":{"contentBottom":"%3Cscript%20async%20src%3D%22//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js%22%3E%3C/script%3E%3C%21--%20%u81EA%u9002%u5E94%u9AD8%u53CD%u5DEE%u9ED1%u8272%20--%3E%3Cins%20class%3D%22adsbygoogle%22%20style%3D%22display%3Ablock%22%20data-ad-client%3D%22ca-pub-9798300025848807%22%20data-ad-slot%3D%228607007877%22%20data-ad-format%3D%22auto%22%3E%3C/ins%3E%3Cscript%3E%28adsbygoogle%20%3D%20window.adsbygoogle%20%7C%7C%20%5B%5D%29.push%28%7B%7D%29%3B%3C/script%3E%3C/p%3E%3Cdiv%20id%3D%22disqus_thread%22%3E%3C/div%3E%3Cscript%3E%28function%28%29%20%7Bvar%20d%20%3D%20document%2C%20s%20%3D%20d.createElement%28%27script%27%29%3Bs.src%20%3D%20%27//webpack-handbook.disqus.com/embed.js%27%3Bs.setAttribute%28%27data-timestamp%27%2C%20+new%20Date%28%29%29%3B%28d.head%20%7C%7C%20d.body%29.appendChild%28s%29%3B%7D%29%28%29%3B%3C/script%3E%3Cnoscript%3EPlease%20enable%20JavaScript%20to%20view%20the%20%3Ca%20href%3D%22https%3A//disqus.com/%3Fref_noscript%22%20rel%3D%22nofollow%22%3Ecomments%20powered%20by%20Disqus.%3C/a%3E%3C/noscript%3E"},"sitemap":{"hostname":"http://zhaoda.net/"},"prism":{},"highlight":{},"search":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <script>var _hmt = _hmt || [];(function() {var hm = document.createElement('script');hm.src = '//hm.baidu.com/hm.js?a9787f0ab45d5e237bab522431d0a7ec';var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(hm, s);})();</script>
    </body>
    
</html>
